import Miner from "../../flyer/Other/Miner";
import { Global } from "../../info/Global";
import { Location } from "../../info/Predefine";
import Facility from "../Facility";

const { ccclass, property } = cc._decorator;

/**
 * 矿地剩余数量条的颜色
 */

/**
 * 矿设施
 */
@ccclass
export default class Mine extends Facility {

    public static EventType = {
        LoseMine: "LoseMine"
    }

    maxCount: number;

    curCount: number;

    @property([cc.SpriteFrame])
    levelFrame: cc.SpriteFrame[] = [];

    progress: cc.ProgressBar = null;

    sprite: cc.Sprite = null;

    /**
     * 当前被占用数量
     */
    takeMiners: Set<Miner>;

    init(locaitons: Location[]) {
        this.maxCount = Global.MineMaxCount;
        this.curCount = this.maxCount;
        this.sprite = this.getComponent(cc.Sprite);
        this.progress = this.getComponent(cc.ProgressBar);
        this.locations = locaitons;
        this.sprite.spriteFrame = this.levelFrame[Math.ceil(this.curCount / this.maxCount * (this.levelFrame.length - 1))]
        this.progress.progress = 1;
        this.takeMiners = new Set<Miner>();
    }

    /**
     * 减少矿数量，返回实际的数量
     * @param count 
     */
    loseCount(count: number): number {
        count = this.curCount - count >= 0 ? count : this.curCount;
        this.curCount = this.curCount - count
        this.progress.progress = this.curCount / this.maxCount;
        this.sprite.spriteFrame = this.levelFrame[Math.ceil(this.curCount / this.maxCount * (this.levelFrame.length - 1))];
        this.node.emit(Mine.EventType.LoseMine, this.curCount)
        return count;
    }

    // protected onDisable(): void {
    //     cc.log("error"); 
    // }

    // protected onDestroy(): void {
    //     cc.log("error"); 
    // }
}
